package com.dfbz.controller.login;

import com.dfbz.controller.BaseServlet;
import com.dfbz.model.User;
import com.dfbz.service.UserServer;
import com.dfbz.service.impl.UserServerImpl;
import com.mysql.fabric.xmlrpc.base.Data;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@WebServlet("/login/*")
public class LoginController extends BaseServlet {

    private UserServer userServer= new UserServerImpl();
    public void  tologin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);
        String username = param.get("form-username");
        String password = param.get("form-password");
        List<User> userList = userServer.toLogin(username, password);
        if (userList.size()>0){
            User user = userList.get(0);
            Date date = new Date();
            user.setLoginTime(date);
            userServer.update(user);
            req.getSession().setAttribute("user",user);
            resp.sendRedirect("/html/home.jsp");
        }else {
            resp.sendRedirect(req.getContextPath()+"/html/index.jsp");
        }

    }
    public void toRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);
        String username = param.get("form-username");
        String password = param.get("form-password");
        String email = param.get("form-email");
        String p2 = param.get("form-password2");
        if (password.equals(p2)){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setEmail(email);
            boolean b = userServer.toRegister(user);
            if (b){
                resp.setCharacterEncoding("UTF-8");
                PrintWriter out = resp.getWriter();
                out.print("<html><head><meta charset='UTF-8'></head>");
                out.println("<script>");
                out.println("alert('注册成功！');");
                out.println("window.location='/'");
                out.println("</script>");
            }else {
                resp.setCharacterEncoding("UTF-8");
                PrintWriter out = resp.getWriter();
                out.print("<html><head><meta charset='UTF-8'></head>");
                out.println("<script>");
                out.println("alert('注册失败！');");
                out.println("window.location='/register.html'");
                out.println("</script>");
            }
        }else {
            resp.setCharacterEncoding("UTF-8");
            PrintWriter out = resp.getWriter();
            out.print("<html><head><meta charset='UTF-8'></head>");
            out.println("<script>");
            out.println("alert('密码不一致！');");
            out.println("window.location='/register.html'");
            out.println("</script>");
        }

    }

    public void wx(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Properties prop = new Properties();
        prop.load(this.getClass().getClassLoader().getResourceAsStream("wx.properties"));
        String appid = prop.getProperty("wx.AppID");
        String redirect_uri = prop.getProperty("wx.redirect_uri");
        String url="https://open.weixin.qq.com/connect/qrconnect?response_type=code"+
                          "&appid=" + appid +
                          "&redirect_uri=" + URLEncoder.encode(redirect_uri) +
                          "&scope=snsapi_login";
        resp.sendRedirect(url);
    }










}
